package cn.com.ddapp.ddm.ui.mine;

import android.app.Dialog;
import android.content.Intent;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.zhy.autolayout.AutoLinearLayout;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.mine.UserData;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TimeCount;
import cn.com.ddapp.ddm.util.ToolFor9Ge;

public class AlterBindMobileActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.old_mobile)
    AutoLinearLayout oldMobile;
    @BindView(R.id.no_old_mobile)
    AutoLinearLayout noOldMobile;

    TextView tvText;
    EditText verify;
    TextView getVerify;
    Button negativeBt1;
    Button positiveBt1;

    String userId;
    UserData userData;
    String mobile;
    View layout;
    TimeCount timeCount;
    Dialog dialog;
    int orientation;


    @Override
    public void initView() {
        titletext.setText(R.string.alter_mobile);
    }

    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);

        OkUtilCallback callback = new OkUtilCallback() {
            @Override
            public void onMyResponse(String response, int id) {
                Gson gson = new Gson();
                userData = gson.fromJson(response, UserData.class);
                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                    BaseApplication.getIntstance().showToast(userData.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else {
                    if (userData.getUser() != null) {
                        mobile = userData.getUser().getMobile();
                    }
                }
            }
        };
        //请求会员资料
        OKUtilAPI.post(OKUtilAPI.getUser, "userId=" + userId, callback);

        LayoutInflater inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.dialog_mobile, (ViewGroup) findViewById(R.id.dialog_mobile));
        layout.setClickable(true);
        tvText = (TextView) layout.findViewById(R.id.tv_text);
        verify = (EditText) layout.findViewById(R.id.verify);
        negativeBt1 = (Button) layout.findViewById(R.id.negative_bt1);
        positiveBt1 = (Button) layout.findViewById(R.id.positive_bt1);
        getVerify = (TextView) layout.findViewById(R.id.getVerify);
        negativeBt1.setClickable(true);
        positiveBt1.setClickable(true);
        verify.setClickable(true);
        getVerify.setClickable(true);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_alter_bind_mobile;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.old_mobile, R.id.no_old_mobile, R.id.back})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            //原手机号码能接受验证码
            case R.id.old_mobile:
                tvText.setText(R.string.tv_text);
                getVerify.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!ToolFor9Ge.isPhoneNO(mobile)) {
                            BaseApplication.getIntstance().showToast("请输入电话号码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                        } else {
                            OkUtilCallback callback = new OkUtilCallback() {
                                @Override
                                public void onMyResponse(String response, int id) {
                                    Gson gson = new Gson();
                                    Entity entity = gson.fromJson(response, Entity.class);
                                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                        BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                        BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                        BaseApplication.getIntstance().showToast("验证码已发送", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                        timeCount = new TimeCount(60000, 1000, getVerify);
                                        timeCount.start();
                                    }
                                }
                            };
                            OKUtilAPI.post(OKUtilAPI.sendVerifyCode, "key=" + mobile, callback);
                        }
                    }
                });

                if (dialog == null) {
                    dialog = new Dialog(this, R.style.FullScreenDialog);
                    dialog.setContentView(layout);
                    dialog.setCanceledOnTouchOutside(true);
                    negativeBt1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                            verify.setText("");
                            if (timeCount!=null){
                                timeCount.cancel();
                                timeCount.onFinish();
                            }

                        }
                    });
                    positiveBt1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String code = verify.getText().toString();
                            if ("".equals(code)){
                                BaseApplication.getIntstance().showToast("请输入验证码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            }else {
                                OkUtilCallback callback = new OkUtilCallback() {
                                    @Override
                                    public void onMyResponse(String response, int id) {
                                        Gson gson = new Gson();
                                        Entity entity = gson.fromJson(response, Entity.class);
                                        if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                            BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                        } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                            tvText.setText(R.string.verify_error);
                                            tvText.setTextColor(getResources().getColor(R.color.color_orange));
                                            BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                        } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                            BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                            Intent i = new Intent(AlterBindMobileActivity.this,OldAlterMobileActivity.class);
                                            i.putExtra("mobile",mobile);
                                            startActivity(i);
                                            finish();
                                            dialog.dismiss();
                                            timeCount.cancel();
                                        }
                                    }
                                };
                                OKUtilAPI.post(OKUtilAPI.verify,"key="+mobile+"&randCode="+code,callback);
                            }
                        }
                    });
                    WindowManager m = getWindowManager();
                    Window dialogWindow = dialog.getWindow();
                    Display d = m.getDefaultDisplay(); // 获取屏幕宽、高
                    WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
                    orientation = getResources().getConfiguration().orientation;
                    if (orientation==0||orientation==2){
                        /*DisplayMetrics dm = new DisplayMetrics();
                        getWindowManager().getDefaultDisplay().getMetrics(dm);
                        int screenWidth = dm.widthPixels;
                        int screenHeigh = dm.heightPixels;
                        p.height = (int) (screenHeigh * 0.5); // 高度设置为屏幕的0.6
                        p.width = (int) (screenWidth * 0.4); // 宽度设置为屏幕的0.65*/
                        p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.6
                        p.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的0.65

                    }else {
                        p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.6
                        //p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65
                    }
                    dialogWindow.setAttributes(p);
                    dialog.show();
                }
                else {
                    dialog.show();
                }
                break;
            //原手机号码不能接受验证码
            case R.id.no_old_mobile:
                intent = new Intent(this, NewAlterMobileActivity.class);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

}
